//
//  LinkDetailPage.m
//  Portable Traffic Signal Assistor
//
//  Created by Ding on 9/2/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//

#import "LinkDetailPage.h"

@interface LinkDetailPage ()

@end

@implementation LinkDetailPage

@synthesize linkNameSection=_linkNameSection;
@synthesize fromIntersectionNameSection=_fromIntersectionNameSection;
@synthesize toIntersectionNameSection=_toIntersectionNameSection;
@synthesize directionSection=_directionSection;
@synthesize lengthSection=_lengthSection;
@synthesize speedLimitSection=_speedLimitSection;
@synthesize linkNameTextView=_linkNameTextView;
@synthesize lengthTextView=_lengthTextView;
@synthesize speedLimitTextView=_speedLimitTextView;
@synthesize link=_link;
@synthesize fromIntersection=_fromIntersection;
@synthesize toIntersection=_toIntersection;
@synthesize agency=_agency;
@synthesize subSystem=_subSystem;
@synthesize sections=_sections;
@synthesize directionSwitch=_directionSwitch;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil link:(Link *)link agency:(Agency *)agency subSystem:(SubSystem *)subSystem
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.link=link;
        self.agency=agency;
        self.subSystem=subSystem;
        self.fromIntersection=[XMLParser getIntersection:link.fromIntersectionID agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
        self.toIntersection=[XMLParser getIntersection:link.toIntersectionID agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
        
    }
    return self;
}

- (void)dealloc
{
    [_linkNameSection release];
    [_fromIntersectionNameSection release];
    [_toIntersectionNameSection release];
    [_directionSection release];
    [_lengthSection release];
    [_speedLimitSection release];
    
    [_linkNameTextView release];
    [_lengthTextView release];
    [_speedLimitTextView release];
    
    [_directionSwitch release];
    
    [_link release];
    [_fromIntersection release];
    [_toIntersection release];
    [_agency release];
    [_subSystem release];
    [_sections release];
    [super dealloc];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.opaque = NO;
    self.tableView.backgroundView = nil;
    
    if(!_linkNameSection)
    {
        _linkNameSection=[[TableSection alloc]init];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _linkNameSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_linkNameSection.contents addObject:self.link.linkName];
        
        _linkNameSection.header=@"Link Name";
    }
    
    if(!_fromIntersectionNameSection)
    {
        _fromIntersectionNameSection=[[TableSection alloc]init];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _fromIntersectionNameSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_fromIntersectionNameSection.contents addObject:self.fromIntersection.intersectionName];
        
        _fromIntersectionNameSection.header=@"From Intersection";
    }
    
    if(!_toIntersectionNameSection)
    {
        _toIntersectionNameSection=[[TableSection alloc]init];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _toIntersectionNameSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_toIntersectionNameSection.contents addObject:self.toIntersection.intersectionName];
        
        _toIntersectionNameSection.header=@"To Intersection";
    }
    
    if(!_directionSection)
    {
        _directionSection=[[TableSection alloc]init];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _directionSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_directionSection.contents addObject:self.link.linkDirection];
        
        _directionSection.header=@"Direction";
    }
    if(!_lengthSection)
    {
        _lengthSection=[[TableSection alloc]init];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _lengthSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_lengthSection.contents addObject:self.link.length];
        
        _lengthSection.header=@"Length (Feet)";
    }
    if(!_speedLimitSection)
    {
        _speedLimitSection=[[TableSection alloc]init];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _speedLimitSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_speedLimitSection.contents addObject:self.link.speedLimit];
        
        _speedLimitSection.header=@"Speed Limit (MPH)";
    }
    if (!_sections) {
        _sections = [[NSMutableArray alloc] init];
        [_sections addObject:self.linkNameSection];
        [_sections addObject:self.fromIntersectionNameSection];
        [_sections addObject:self.toIntersectionNameSection];
        [_sections addObject:self.directionSection];
        [_sections addObject:self.lengthSection];
        [_sections addObject:self.speedLimitSection];
    }
    
    UITapGestureRecognizer *gestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textViewReturn:)] autorelease];
    gestureRecognizer.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:gestureRecognizer];
    
    [self.navigationController setToolbarHidden:NO];
    CustomizedBarButtonItem *homeButtonItem=[[[CustomizedBarButtonItem alloc] initCustomizedBarButtonItem:[UIImage imageNamed:@"home.png"] target:self action:@selector(goToHome:)] autorelease];
    self.toolbarItems = [ NSArray arrayWithObjects: homeButtonItem,nil ];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.linkNameSection=nil;
    self.fromIntersectionNameSection=nil;
    self.toIntersectionNameSection=nil;
    self.directionSection=nil;
    self.lengthSection=nil;
    self.speedLimitSection=nil;
    
    self.linkNameTextView=nil;
    self.lengthTextView=nil;
    self.speedLimitTextView=nil;
    self.sections=nil;
    self.directionSwitch=nil;
    
//    self.link=nil;
//    self.fromIntersection=nil;
//    self.toIntersection=nil;
//    self.agency=nil;
//    self.subSystem=nil;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self updateView];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return [self.sections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sectionContents=[[self.sections objectAtIndex:section] contents];
    
    return [sectionContents count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
    // Configure the cell...
    TableSection *section=[self.sections objectAtIndex:indexPath.section];
    NSArray *sectionContents=[section contents];
    NSString *sectionContentString=[sectionContents objectAtIndex:indexPath.row];
    
    
    [cell setAccessoryType:[section accessoryType]];
    cell.textLabel.numberOfLines=0;
    cell.textLabel.lineBreakMode=UILineBreakModeWordWrap;
    cell.textLabel.text=sectionContentString;
    
    switch (indexPath.section) {
        case 0:
        {
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            
            CGFloat height=[TableSection calculateStringHeight:[[section contents] objectAtIndex:indexPath.row]];
            self.linkNameTextView=[[[UITextView alloc] initWithFrame:CGRectMake(1, 5, (CONST_portraitCellWidth-2), (height-10))] autorelease];
            [self.linkNameTextView setText:sectionContentString];
            [self.linkNameTextView setFont:[TableSection TitleFont]];
            //            [self.projectNameTextView setTag:ProjectNameTextView];
            self.linkNameTextView.delegate=self;
            
            [[cell contentView] addSubview:self.linkNameTextView];
            [cell setBackgroundColor:[TableSection WhiteColor]];
            
            return cell;
        }
            
            break;
        case 3:
        {
            CGFloat height=[TableSection calculateStringHeight:[[section contents] objectAtIndex:indexPath.row]];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
//            self.directionSwitch=[[[UISwitch alloc] initWithFrame:CGRectMake((CONST_portraitCellWidth*3/4), 5, (CONST_portraitCellWidth/4), (height-10))] autorelease];
//            if ([self.link.linkDirection isEqualToString:@"Two Way"]) {
//                [self.directionSwitch setOn:YES];
//            }else
//            {
//                [self.directionSwitch setOn:NO];
//            }
//            [self.directionSwitch addTarget:self action:@selector(setDirection:) forControlEvents:UIControlEventTouchUpInside];
//            [[cell contentView] addSubview:self.directionSwitch];
            int myIndentLevel=15;
            UILabel* myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10*myIndentLevel, 0, 320-(10*myIndentLevel), height)] autorelease];
            
            myLabel.text = @"Click to Switch";
            myLabel.textColor=[UIColor grayColor];
            myLabel.backgroundColor=[UIColor clearColor];
            [cell.contentView addSubview:myLabel];
        }
            break;
        case 4:
        {
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            
            CGFloat height=[TableSection calculateStringHeight:[[section contents] objectAtIndex:indexPath.row]];
            self.lengthTextView=[[[UITextView alloc] initWithFrame:CGRectMake(1, 5, (CONST_portraitCellWidth-2), (height-10))] autorelease];
            [self.lengthTextView setText:sectionContentString];
            [self.lengthTextView setFont:[TableSection TitleFont]];
            //            [self.projectNameTextView setTag:ProjectNameTextView];
            self.lengthTextView.delegate=self;
            
            [[cell contentView] addSubview:self.lengthTextView];
            [cell setBackgroundColor:[TableSection WhiteColor]];
            
            return cell;
        }
            
            break;
        case 5:
        {
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            
            CGFloat height=[TableSection calculateStringHeight:[[section contents] objectAtIndex:indexPath.row]];
            self.speedLimitTextView=[[[UITextView alloc] initWithFrame:CGRectMake(1, 5, (CONST_portraitCellWidth-2), (height-10))] autorelease];
            [self.speedLimitTextView setText:sectionContentString];
            [self.speedLimitTextView setFont:[TableSection TitleFont]];
            //            [self.projectNameTextView setTag:ProjectNameTextView];
            self.speedLimitTextView.delegate=self;
            
            [[cell contentView] addSubview:self.speedLimitTextView];
            [cell setBackgroundColor:[TableSection WhiteColor]];
            
            return cell;
        }
            
            break;
        
            
        default:
            cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
            break;
    }

    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    CGRect headerViewRect = CGRectMake(0.0,0.0,320,40);
    UIView* headerView = [[[UIView alloc] initWithFrame:headerViewRect] autorelease];
    headerView.backgroundColor = [UIColor clearColor];
    UILabel *aLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20,0.0,300,40)] autorelease];
    aLabel.text =[[self.sections objectAtIndex:section] header] ;
    aLabel.font = [UIFont boldSystemFontOfSize:16.0];
    aLabel.textAlignment = UITextAlignmentLeft;
    aLabel.backgroundColor = [UIColor clearColor];
    aLabel.textColor=[UIColor darkGrayColor];
    
    
    [headerView addSubview:aLabel];
    switch (section) {
        case 0:
        case 1:
        {
            return headerView;
        }
            break;
            
        default:
        {
            //Then you can add a UILabel to your headerView
            //            UIButton *newbutton=[UIButton buttonWithType:UIButtonTypeContactAdd];
            //            [newbutton setFrame:CGRectMake(160,4,30,30)];
            //            //    [newbutton setTitle:@"Remove" forState:UIControlStateNormal];
            //            //        NSArray *timerGroupList=[XMLParser getTimerGroupList];
            //            [newbutton setTag:(section)];
            //            [newbutton addTarget:self action:@selector(clickAddSubSystemButton:)
            //                forControlEvents:UIControlEventTouchUpInside];
            //            [headerView addSubview:newbutton];
        }
            break;
    }
    
    
    
    
    return headerView;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    
    return [[self.sections objectAtIndex:section] header];
    
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
    switch (indexPath.section) {
            
        case 1:
        {
            IntersectionDetailPage *detailViewController=[[[IntersectionDetailPage alloc] initWithNibNameAndIntersection:@"IntersectionDetailPage" agency:self.agency subSystem:self.subSystem intersection:self.fromIntersection bundle:nil] autorelease];
            [detailViewController setTitle:@"Intersection Detail"];
            [self.navigationController pushViewController:detailViewController animated:YES];
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        }
            break;
        case 2:
        {
            IntersectionDetailPage *detailViewController=[[[IntersectionDetailPage alloc] initWithNibNameAndIntersection:@"IntersectionDetailPage" agency:self.agency subSystem:self.subSystem intersection:self.toIntersection bundle:nil] autorelease];
            [detailViewController setTitle:@"Intersection Detail"];
            [self.navigationController pushViewController:detailViewController animated:YES];
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        }
            break;
        case 3:
        {
            if ([self.link.linkDirection isEqualToString:@"One Way"]) {
                self.link.linkDirection=@"Two Way";
            }else
            {
                self.link.linkDirection=@"One Way";
            }
            NSLog(@"\n%@",self.link.linkDirection);
            [XMLParser updateLink:self.link agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
            self.directionSection.contents=[[[NSMutableArray alloc] init] autorelease];
            [self.directionSection.contents addObject:self.link.linkDirection];
            [self.tableView reloadData];
        }
            break;
        default:
            break;
    }
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
            
        case 1:
        case 2:
        {
//            NSArray *intersectionList=[XMLParser getIntersectionList:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
//            NSMutableArray * data1=[[[NSMutableArray alloc] init] autorelease];
//            NSMutableArray * data2=[[[NSMutableArray alloc]init] autorelease];
//            for (int i=0; i<intersectionList.count; i++) {
//                Intersection *intersection=[intersectionList objectAtIndex:i];
//                [data1 addObject:intersection.intersectionName];
//                [data2 addObject:intersection.intersectionName];
//            }
            NSArray * data1=[XMLParser getIntersectionList:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
            NSArray * data2=[XMLParser getIntersectionList:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
            NSMutableArray *dataArray=[[[NSMutableArray alloc] init] autorelease];
            [dataArray addObject:data1];
            [dataArray addObject:data2];
            PickerViewController *pickerViewController=[[[PickerViewController alloc] initWithNibName:@"PickerViewController" bundle:nil dataArray:dataArray link:self.link agency:self.agency subSystem:self.subSystem] autorelease];
            [pickerViewController setTitle:@"Select From To Intersection"];
            [self.navigationController pushViewController:pickerViewController animated:YES];
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        }
            
        default:
            break;
    }
}
#pragma mark -TextView Delegate
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    self.link.linkName=self.linkNameTextView.text;
    self.link.length=self.lengthTextView.text;
    self.link.speedLimit=self.speedLimitTextView.text;
    
    [XMLParser updateLink:self.link agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
    
//    if(self.linkNameSection)
//    {
//        
//        self.linkNameSection.contents=[[[NSMutableArray alloc] init] autorelease];
////        self.linkNameSection.contents=[[[NSMutableArray alloc] init] autorelease];
//        [self.linkNameSection.contents addObject:self.link.linkName];
//        
////        self.linkNameSection.header=@"Intersection Name";
//    }
//    if(self.lengthSection)
//    {
//        self.lengthSection=[[[TableSection alloc]init] autorelease];
//        
//        self.lengthSection.contents=[[[NSMutableArray alloc] init] autorelease];
//        [self.lengthSection.contents addObject:self.link.length];
//        
////        self.nameSection.header=@"Intersection Coordinate";
//    }
//    if(self.speedLimitSection)
//    {
//        self.speedLimitSection=[[[TableSection alloc]init] autorelease];
//        self.speedLimitSection.contents=[[[NSMutableArray alloc] init] autorelease];
//        
//        [self.speedLimitSection.contents addObject:self.link.speedLimit];
//        
//        //        self.nameSection.header=@"Intersection Coordinate";
//    }
    return YES;
}
#pragma mark -customized functions
- (IBAction)textViewReturn:(id)sender
{
    [self.linkNameTextView resignFirstResponder];
    [self.lengthTextView resignFirstResponder];
    [self.speedLimitTextView resignFirstResponder];
    
    
}
- (void) goToHome:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)setDirection:(id)sender {
    BOOL state = [sender isOn];
    self.link.linkDirection = state == YES ? @"Two Way" : @"One Way";
    NSLog(@"\n%@",self.link.linkDirection);
    [XMLParser updateLink:self.link agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
    self.directionSection.contents=[[[NSMutableArray alloc] init] autorelease];
    [self.directionSection.contents addObject:self.link.linkDirection];
    [self.tableView reloadData];
    
}

- (void) updateView
{
    self.link=[XMLParser getLink:self.link.linkID agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
    self.fromIntersection=[XMLParser getIntersection:self.link.fromIntersectionID agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
    self.toIntersection=[XMLParser getIntersection:self.link.toIntersectionID agencyID:self.agency.agencyID subSystemID:self.subSystem.subSystemID];
    if(self.linkNameSection)
    {
        self.linkNameSection=[[[TableSection alloc]init] autorelease];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _linkNameSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_linkNameSection.contents addObject:self.link.linkName];
        
        _linkNameSection.header=@"Link Name";
    }
    
    if(self.fromIntersectionNameSection)
    {
        self.fromIntersectionNameSection=[[[TableSection alloc]init] autorelease];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _fromIntersectionNameSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_fromIntersectionNameSection.contents addObject:self.fromIntersection.intersectionName];
        
        _fromIntersectionNameSection.header=@"From Intersection";
    }
    
    if(self.toIntersectionNameSection)
    {
        self.toIntersectionNameSection=[[[TableSection alloc]init] autorelease];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _toIntersectionNameSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_toIntersectionNameSection.contents addObject:self.toIntersection.intersectionName];
        
        _toIntersectionNameSection.header=@"To Intersection";
    }
    
    if(self.directionSection)
    {
        self.directionSection=[[[TableSection alloc]init] autorelease];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _directionSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_directionSection.contents addObject:self.link.linkDirection];
        
        _directionSection.header=@"Direction";
    }
    if(self.lengthSection)
    {
        self.lengthSection=[[[TableSection alloc]init] autorelease];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _lengthSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_lengthSection.contents addObject:self.link.length];
        
        _lengthSection.header=@"Length (Feet)";
    }
    if(self.speedLimitSection)
    {
        self.speedLimitSection=[[[TableSection alloc]init] autorelease];
        
        //        _agencySection.contents=[[[NSMutableArray alloc] initWithArray:[XMLParser getAgencyList]] autorelease];
        _speedLimitSection.contents=[[[NSMutableArray alloc] init] autorelease];
        [_speedLimitSection.contents addObject:self.link.speedLimit];
        
        _speedLimitSection.header=@"Speed Limit (MPH)";
    }
    if (self.sections) {
        self.sections = [[[NSMutableArray alloc] init] autorelease];
        [_sections addObject:self.linkNameSection];
        [_sections addObject:self.fromIntersectionNameSection];
        [_sections addObject:self.toIntersectionNameSection];
        [_sections addObject:self.directionSection];
        [_sections addObject:self.lengthSection];
        [_sections addObject:self.speedLimitSection];
    }
    [self.tableView reloadData];
}

@end
